﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace CarBlackBox
{
    public partial class Form_VehicleEditor : Form
    {
        private VehicleInfoData Vehicle=new VehicleInfoData();
        bool bClose = true;
        Form_VehicleManage F;
        public Form_VehicleEditor(Form_VehicleManage f)
        {
            InitializeComponent();
            F = f;
        }
        public Form_VehicleEditor(VehicleInfoData V)
        {
            InitializeComponent();
            Vehicle=V;            
        }
        private void Form_VehicleEditor_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“systemDBDataSet.RecordInfo”中。您可以根据需要移动或移除它。
            this.recordInfoTableAdapter.Fill(this.systemDBDataSet.RecordInfo);
            // TODO: 这行代码将数据加载到表“systemDBDataSet.CarType”中。您可以根据需要移动或移除它。
            this.carTypeTableAdapter.Fill(this.systemDBDataSet.CarType);
            if (cbRIN.Items.Count == 0)
            {
                MessageBox.Show("当前并未添加过任何记录仪，无法添加车辆信息,请先添加记录信息");
                this.Close();
            }
            if (string.IsNullOrEmpty(Vehicle.LicensePlate))//添加
            {
                cbCarType.SelectedIndex = 0;
                cbRIN.SelectedIndex = 0;
                cbCheckMonth.SelectedIndex = 0;
                this.Text = this.Text + "新增车辆资料";
            }
            else//修改
            {
                tbLicensePlate.ReadOnly = true;
                SetVehicleInfo();
                this.Text = this.Text + "修改" + Vehicle.LicensePlate + "车辆资料";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Vehicle.LicensePlate))//添加
            {
                if (GetVehicleInfo())
                {
                    int n=0;                   
                    n = vehicleInfoTableAdapter1.InsertQuery(Vehicle.LicensePlate,
                                                    Vehicle.VIN,
                                                    Vehicle.CarTypeID,
                                                    Vehicle.Factor,
                                                    Vehicle.Sensor,
                                                    Vehicle.OilPer,
                                                    Vehicle.RIN,
                                                    Vehicle.Brand,
                                                    Vehicle.Model,
                                                    Vehicle.BuyDate,
                                                    Vehicle.CheckMonth,
                                                    Vehicle.Memo,
                                                    Vehicle.Operator,
                                                    Vehicle.LastUpdateTime);                    
                    if (n != 1)
                        MessageBox.Show("存储失败，请重新尝试或联系管理员!!!!");
                    else
                    {
                        F.RenovateVehicleInfo();
                        WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                        log.LogType = LogType.车辆资料管理.ToString();
                        log.LogContent = "添加车牌号为" + Vehicle.LicensePlate + "的车辆资料";
                        log.LogRemark = "车辆资料添加";
                        log.InserSQLLog();
                        if (WFNetLib.WFGlobal.MessageYseOrNo("存储成功\r\nYes继续添加\r\nNo退出添加", "成功") == DialogResult.Yes)
                        {
                            Vehicle = new VehicleInfoData();
                            SetVehicleInfo(); 
                        }
                        else
                        {
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                }
            }
            else//修改
            {
                if (GetVehicleInfo())
                {
                    int n;
                    n = vehicleInfoTableAdapter1.UpdateQuery(   Vehicle.VIN,
                                                                Vehicle.CarTypeID,
                                                                Vehicle.Factor,
                                                                Vehicle.Sensor,
                                                                Convert.ToDecimal(Vehicle.OilPer),
                                                                Vehicle.RIN,
                                                                Vehicle.Brand,
                                                                Vehicle.Model,
                                                                Vehicle.BuyDate,
                                                                Vehicle.CheckMonth,
                                                                Vehicle.Memo,
                                                                Vehicle.Sign,
                                                                Vehicle.LimitSpeed,
                                                                Vehicle.RecordTime,
                                                                Vehicle.SuperTime,
                                                                Vehicle.TiredTime,
                                                                Vehicle.Setup,
                                                                Vehicle.RestTime,
                                                                Vehicle.Operator,
                                                                Vehicle.LastUpdateTime,
                                                                Vehicle.LicensePlate
                                                            );                        
                    if (n != 1)
                        MessageBox.Show("存储失败，请重新尝试或联系管理员!!!!");
                    else
                    {
                        MessageBox.Show("修改成功!");
                        WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                        log.LogType = LogType.车辆资料管理.ToString();
                        log.LogContent = "修改车牌号为" + Vehicle.LicensePlate + "的车辆资料";
                        log.LogRemark = "车辆资料修改";
                        log.InserSQLLog();
                        this.DialogResult = DialogResult.OK;                        
                    }
                }
            }
        }
        private bool GetVehicleInfo()
        {
            if (string.IsNullOrEmpty(tbLicensePlate.Text))
            {
                tbLicensePlate.Focus();
                WFNetLib.WFGlobal.MessageWaring("车牌号码不能为空", "缺少关键参数");
                return false;
            }
            Vehicle.LicensePlate = tbLicensePlate.Text;
            if (string.IsNullOrEmpty(tbVIN.Text))
            {
                tbVIN.Focus();
                WFNetLib.WFGlobal.MessageWaring("车辆识别码(VIN)不能为空", "缺少关键参数");
                return false;
            }
            Vehicle.VIN = tbVIN.Text;           
            Vehicle.CarTypeID = byte.Parse(cbCarType.SelectedValue.ToString());
            Vehicle.RIN = cbRIN.Text;
            Vehicle.Brand = tbBrand.Text;
            Vehicle.Model = tbModel.Text;
            Vehicle.CheckMonth = cbCheckMonth.Text;
            if (dtpBuyDate.Checked)
                Vehicle.BuyDate = dtpBuyDate.Value.ToLongDateString();
            else
                Vehicle.BuyDate = "无";
            Vehicle.Factor = Convert.ToInt32(nFactor.Value);
            Vehicle.Sensor = Convert.ToByte(nSensor.Value);
            Vehicle.OilPer = Convert.ToSingle(nOilPer.Value);
            Vehicle.Memo = WFNetLib.Strings.StringsFunction.strEncode(tbMemo.Text);
            Vehicle.Operator = Form_Main.Operator;
            return true;
        }
        private void SetVehicleInfo()
        {            
            tbLicensePlate.Text=Vehicle.LicensePlate;            
            tbVIN.Text=Vehicle.VIN;
            cbCarType.SelectedValue=Vehicle.CarTypeID;
            if (string.IsNullOrEmpty(Vehicle.RIN))
                cbRIN.SelectedIndex = 0;
            else
            {
                cbRIN.Text = Vehicle.RIN;
            }
            tbBrand.Text=Vehicle.Brand;
            tbModel.Text=Vehicle.Model;
            cbCheckMonth.Text=Vehicle.CheckMonth;
            if (Vehicle.BuyDate == "无")
                dtpBuyDate.Checked = false;
            else
            {
                dtpBuyDate.Checked = true;
                dtpBuyDate.Value = DateTime.Parse(Vehicle.BuyDate);                 
            }
            nFactor.Value=Vehicle.Factor;           
            nSensor.Value=Vehicle.Sensor;
            nOilPer.Value=Convert.ToDecimal(Vehicle.OilPer);
            tbMemo.Text = WFNetLib.Strings.StringsFunction.strDecode(Vehicle.Memo);
            tbOperator.Text = Vehicle.Operator;
            tbLastUpdateTime.Text = Vehicle.LastUpdateTime.ToString();
        }

        private void nFactor_ValueChanged(object sender, EventArgs e)
        {
            tbK.Text = (nFactor.Value * nSensor.Value).ToString();
        }

        private void nSensor_ValueChanged(object sender, EventArgs e)
        {
            tbK.Text = (nFactor.Value * nSensor.Value).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (WFNetLib.WFGlobal.MessageYseOrNo("确定取消当前操作么？", "驾驶员信息编辑取消") == DialogResult.No)
                bClose = false;
        }

        private void Form_VehicleEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!bClose)
            {
                e.Cancel = true;
                bClose = true;
            }
        }
    }
}
